Home:ALL Converter>How do I show the results of my [Django] model in [HTML] from a [PostgreSQL] database?

How do I show the results of my [Django] model in [HTML] from a [PostgreSQL] database?

Ask Time:2018-09-18T09:32:26         Author:Tristan

Json Formatter

I am developing a web application with Django and PostgreSQL support. I am using Psycopg2 to connect with my PostgreSQL database. I am attempting to list all of the tables in my database on my Home.html document.

I am having trouble showing this in the Home.html document. Below I have included my code.

models.py

from django.db import models
import psycopg2


# Create your models here.def __init__(self, db='giquery-data'):

class Tables(models.Model):
    conn = psycopg2.connect("dbname='giquery-data' user='postgres' host='localhost' password='admin'")
    cur = conn.cursor()
    # """SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='public'"""
    cur.execute("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='public'")
    rows = cur.fetchall()

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Tables
from django.template import loader

# Create your views here.


def index(request):
    all_tables = Tables.rows
    template = loader.get_template('tablesearch/home.html')
    context = {
        'all_tables': all_tables
    }
    return HttpResponse(template.render(context, request))

Home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul>
    {% for rows in all_tables %}
    <li>
         {{ rows.table_catalog }} - {{ rows.table_name }} Test
    </li>
    {% endfor %}
</ul>


</body>
</html>

Currently, my results look like:

enter image description here

How can I show the names of the database tables on Home.html?

Author:Tristan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/52377710/how-do-i-show-the-results-of-my-django-model-in-html-from-a-postgresql-dat
yy